<?php
/**
 * LinkButcher - Self-hosted URLs Shortener
 *
 * Copyright (c) 2009 Axonaris Marek Szewczyk www.axonaris.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @link http://linkbutcher.googlecode.com
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 * @copyright  Copyright (c) 2009 Axonaris Marek Szewczyk www.axonaris.com
 */

/*
 * Some helpful functions collected in class.
 */

class LinkButcher {

    const VERSION = '0.0.1';

    private $dbh;

    function run() {
        $request = $this->getRequestPath();
        $this->openDb();
        $this->dispatch($request);
    } // run

    function dispatch($request) {
        switch ($request) {
            case '':
                echo 'main page';
                break;
            case 'admin':
                echo 'admin area';
                break;
            default:
                echo "Unable to resolve requested <b>$request</b>";
                break;
        }
    } // dispatch

    /**
     * Opens SQLite connection and checks DB.
     * 
     * If DB is empty - database structure id created.
     * If DB needs update - it is updated.
     */
    function openDb() {
        $this->dbh = new PDO('sqlite:'.CFG_DB_FILE);
        $res = $this->dbh->query("SELECT name
                                  FROM sqlite_master
                                  WHERE type='table'
                                  ORDER BY name");
        //print_r($this->dbh->errorInfo());
        $data = $res->fetch(PDO::FETCH_ASSOC);
        var_dump($res);
        var_dump($data);
    } // openDb

    /**
     * Checks installation.
     */
    private function isInstalled() {
        ;
    } // _is_installed

    private function getRequestPath() {
        return trim($_SERVER['REDIRECT_URL'], '/');
    } // _getRequestPath

} // LinkButcher
